<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1Order;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 订单列表
 */
class V1OrderController extends Crud
{

    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需鉴权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select', 'remark'
    ];

    /**
     * @var V1Order
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1Order;
    }

    /**
     * 浏览
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $user_id = $request->input('user_id');
        return view('v1-order/index',compact('user_id'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $where['deleted'] = CommonConstant::DELETE_0;
        $query = $this->doSelect($where, $field, $order);
        $query->withCount([
            'orderNode as number' => function ($query) {
                $query->where('deleted', CommonConstant::DELETE_0)
                    ->whereIn('refund_status', [CommonConstant::DEFAULT_0, CommonConstant::DEFAULT_3])
                    ->select($query->raw("count(id)"));
            },
            'orderNode as total_amount' => function ($query) {
                $query->where('deleted', CommonConstant::DELETE_0)
                    ->whereIn('refund_status', [CommonConstant::DEFAULT_0, CommonConstant::DEFAULT_3])
                    ->select($query->raw("sum(amount)"));
            }
        ]);
        $paginator = $query->paginate($limit);
        $total = $paginator->total();
        $items = $paginator->items();
        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }


    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-order/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-order/update');
    }

    /**
     * 备注
     * @param Request $request
     * @return Response
     * @throws BusinessException
     **/
    public function remark(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $primary_key = $this->model->getKeyName();
            $ids = $request->post($primary_key);
            $remark = $request->post('remark');
            if (!$ids) {
                return $this->json(1, '订单参数错误！');
            }
            if (!$remark) {
                return $this->json(1, '请输入备注！');
            }
            $this->model->whereIn($primary_key, $ids)->update(['remark' => $remark]);
            return $this->json(0, '操作成功！');
        }
    }

}
